package com.bba.manager.sku.controller;

import com.bba.manager.common.model.sku.CarVendor;
import com.bba.manager.common.model.sku.Parts;
import com.bba.manager.common.model.sku.PartsVendor;
import com.bba.manager.common.util.ResponseEnum;
import com.bba.manager.common.util.ResponseVO;
import com.bba.manager.sku.service.impl.PartsServiceImpl;
import com.bba.manager.sku.service.impl.PartsVendorServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 厂商配件表 前端控制器
 * </p>
 *
 * @author SYANDZZY
 * @since 2025-08-12
 */
@RestController
@RequestMapping("/partsVendor")
public class PartsVendorController {
    @Autowired
    private PartsVendorServiceImpl partsVendorService;
    @GetMapping("/getByPartsId")
    public ResponseVO getByCarId(@RequestParam Long id){
        return ResponseVO.success(partsVendorService.getByCarId(id));
    }
    @PostMapping("/decount/{id}/{num}")
    public ResponseVO decount(
            @PathVariable("id") Long id,
            @PathVariable("num") Integer num
    ){
        PartsVendor ps = partsVendorService.getByCarId(id);
        if(ps.getPartsNum() < num){
            return ResponseVO.success(ResponseEnum.STOCK_NO_MANY_NUMBER.getMessage());
        }
        ps.setPartsNum(ps.getPartsNum()-num);
        partsVendorService.updateById(ps);
        return ResponseVO.success();
    }
    @PostMapping("/add")
    public ResponseVO add(PartsVendor parts){
        return ResponseVO.success(partsVendorService.save(parts));
    }
    @DeleteMapping("/deleted")
    public ResponseVO deleted(Long id){
        return ResponseVO.success(partsVendorService.removeById(id));
    }
    @PostMapping("/update")
    public  ResponseVO update(PartsVendor parts){
        return ResponseVO.success(partsVendorService.updateById(parts));
    }
    @GetMapping("/getone")
    public ResponseVO getone(Long id){
        return ResponseVO.success(partsVendorService.getById(id));
    }
    @GetMapping("/getall")
    public ResponseVO getall(){
        return ResponseVO.success(partsVendorService.list());
    }

}
